// Copyright (c) 2023 Presto Labs Pte. Ltd.
// Author: ziyan
#include "coin2/exchange/base/feed/ws/bittrex_subscriber.h"

#include "coin2/base/log.h"
#include "coin2/exchange/base/feed/ws/bittrex_connection.h"

namespace coin2::exchange::base::feed {
using coin2::exchange::feed::WebsocketConfig;

void BittrexSubscriber::ConnectForSymbolGroupAndWorker(
    const WsConnectionConfig* connection_config) {
  std::weak_ptr<WsSubscriber> self(shared_from_this());
  std::shared_ptr<LatencyExtractor> extractor(nullptr);
  if (config_.reconnect_slowest() || config_.monitor_connection()) {
    extractor = latency_manager_.AddLatencyExtractor(connection_config);
  }
  std::shared_ptr<BittrexConnection> conn = std::make_shared<BittrexConnection>(
      *connection_config,
      [self, connection_config] {
        if (auto s = self.lock()) {
          boost::asio::post(s->strand_, [self, connection_config] {
            if (auto s = self.lock()) {
              s->latency_manager_.RemoveLatencyExtractor(connection_config);
              s->AddForSymbolGroupAndWorker(connection_config);
            } else {
              LOG(ERROR) << "boost::asio handler object (WsSubscriber) does not exist";
            }
          });
        } else {
          LOG(ERROR) << "boost::asio handler object (WsSubscriber) does not exist";
        }
      },
      flow_sub_,
      request_generator_.get(),
      extractor,
      enable_telemetry_);
  conn->RunAsync();
  {
    std::scoped_lock _(connections_mutex_);
    ConnectionKey k(connection_config->connection_id, connection_config->worker_id);
    connections_.insert({k, conn});
  }
}

}  // namespace coin2::exchange::base::feed
